﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CUETools.Codecs;
using CUETools.Codecs.FLAKE;
using System.IO;

namespace SpeechToText.STT
{
    public class FlacResouce
    {
        #region Publuc Values

        public WavResource Parent { get; private set; }

        #endregion

        #region Private Values

        private byte[] Data;

        #endregion
        #region Constructors and Destructors

        public FlacResouce(byte[] Data, WavResource Parent)
        {
            this.Data = Data;
            this.Parent = Parent;
        }

        public FlacResouce(WavResource Parent)
        {
            this.Parent = Parent;
            Convert(Parent);
        }

        ~FlacResouce()
        {
            Data = null;
        }
        #endregion

        #region PrivateMethods

        public void Convert(WavResource WavResourse)
        {
            IAudioSource audioSource = new WAVReader("",new MemoryStream(WavResourse.ToArray()));
            AudioBuffer buff = new AudioBuffer(audioSource, 0x10000);

            MemoryStream S = new MemoryStream();
            FlakeWriter flakewriter = new FlakeWriter("", S, audioSource.PCM);

            FlakeWriter audioDest = flakewriter;
            while (audioSource.Read(buff, -1) != 0)
            {
                audioDest.Write(buff);
            }
            audioDest.Close();

            Data = S.ToArray();
            S.Close();
        }
        
        #endregion

        #region Public Methods

        public byte[] ToArray()
        {
            return Data;
        }

        #endregion
    }
}
